//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Policy Injection Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.Design;

namespace EntLibContrib.PolicyInjection.Configuration.Design
{
	/// <summary>
	/// Policy Injection Configuration Design Manager
	/// </summary>
	public class PolicyInjectionConfigurationDesignManager : ConfigurationDesignManager
	{
		#region Public Methods
		/// <summary>
		/// When overridden by a class, Register any mechanisms needed for the design manager into the design time.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public override void Register(IServiceProvider serviceProvider)
		{
			PolicyInjectionNodeMapRegistrar nodeMapRegistrar = new PolicyInjectionNodeMapRegistrar(serviceProvider);
			nodeMapRegistrar.Register();

			PolicyInjectionCommandRegistrar commandRegistrar = new PolicyInjectionCommandRegistrar(serviceProvider);
			commandRegistrar.Register();
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// When overriden by a class, allows design managers to perform specific operations when opening configuration.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <param name="rootNode">The root node of the application.</param>
		/// <param name="section">The <see cref="T:System.Configuration.ConfigurationSection"/> that was opened from the <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource"/>.</param>
		protected override void OpenCore(IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, System.Configuration.ConfigurationSection section)
		{
			foreach (MatchingRuleCollectionNode matchingRuleNode in rootNode.Hierarchy.FindNodesByType(rootNode, typeof(MatchingRuleCollectionNode)))
			{
				new PolicyInjectionNodeBuilder(serviceProvider).BuildAdditionalMatchingRuleNodes(matchingRuleNode);
			}
			base.OpenCore(serviceProvider, rootNode, section);
		}
		#endregion
	}
}
